/**
 * CastDice.java
 *
 * Created on Apr 30, 2010, 4:44:24 PM
 * 
 * @author Beatrice Bacelli
 * @author Luca Bedogni
 * @author Silvia Righini
 */

package unibo.lsb.graphic;

import java.awt.BorderLayout;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;

import unibo.lsb.logic.CoveredDice;
import unibo.lsb.logic.Dice;
import unibo.lsb.logic.PlayerId;

/**
 * 
 */
public class CastDiceDialog extends javax.swing.JDialog {

	private javax.swing.JPanel firstRow;
	private javax.swing.JPanel secondRow;
	private javax.swing.JPanel thirdRow;
	private javax.swing.JButton btn_castDice;
	private javax.swing.JButton btn_ok;
	private javax.swing.JLabel lbl_dice1;
	private javax.swing.JLabel lbl_dice2;
	private javax.swing.JLabel lbl_text;
	private javax.swing.JComboBox cb_dice;

	Dice dependingDice;
	Dice newDice;
	Dice newDiceDeclared;

	Boolean diceCast = false;
	List<Dice> listOfDice = new ArrayList<Dice>();
	GameTableWindow parent;
	GraphicUtils graphicUtils = new GraphicUtils();

	/** Creates new form CastDice */
	public CastDiceDialog(GameTableWindow parent, boolean modal,
			Dice dependingDice) {
		super(parent, modal);
		this.parent = parent;
		this.dependingDice = dependingDice;
		initComponents();
	}

	/**
	 * Initialization
	 */
	private void initComponents() {

		firstRow = new javax.swing.JPanel();
		secondRow = new javax.swing.JPanel();
		thirdRow = new javax.swing.JPanel();

		lbl_dice1 = new javax.swing.JLabel();
		lbl_dice2 = new javax.swing.JLabel();
		btn_castDice = new javax.swing.JButton();
		lbl_text = new javax.swing.JLabel();

		getListForComboBox();
		cb_dice = new JComboBox(listOfDice.toArray());
		cb_dice.setSelectedIndex(0);

		btn_ok = new javax.swing.JButton();

		setTitle("Cast dice");
		setResizable(false);
		setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				if (diceCast) {
					close(ConfigurationConstGraphics.MINIMAL_CLOSE);
				} else {
					close(ConfigurationConstGraphics.TOTAL_CLOSE);
				}
			}
		});

		lbl_dice1.setText("");
		lbl_dice1.setIcon(graphicUtils.getDiceIcon(0));
		lbl_dice2.setText("");
		lbl_dice2.setIcon(graphicUtils.getDiceIcon(0));

		btn_castDice.setText("cast");
		btn_castDice.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btn_castDiceActionPerformed(evt);
			}
		});

		lbl_text.setText("You declare..");

		btn_ok.setText("ok");
		btn_ok.setEnabled(false);
		btn_ok.setToolTipText("You can't confirm if you don't cast!");
		btn_ok.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				btn_okActionPerformed(evt);
			}
		});

		// Layout
		FlowLayout firstRowLayout = new FlowLayout(FlowLayout.CENTER, 25, 10);
		firstRowLayout.setAlignment(FlowLayout.TRAILING);
		firstRow.setLayout(firstRowLayout);

		firstRow.add(btn_castDice);
		firstRow.add(lbl_dice1);
		firstRow.add(lbl_dice2);
		firstRow.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);

		secondRow.setLayout(new FlowLayout(FlowLayout.CENTER, 25, 20));
		secondRow.add(lbl_text);
		secondRow.add(cb_dice);

		thirdRow.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 10));
		thirdRow.add(btn_ok);

		Container pane = this.getContentPane();
		pane.add(firstRow, BorderLayout.NORTH);
		pane.add(secondRow, BorderLayout.CENTER);
		pane.add(thirdRow, BorderLayout.SOUTH);

		pack();
		setLocationRelativeTo(parent);
	}

	/**
	 * Cast dice.
	 * 
	 * @param evt
	 */
	private void btn_castDiceActionPerformed(java.awt.event.ActionEvent evt) {
		if (!diceCast) {
			newDice = new Dice();
			lbl_dice1.setIcon(graphicUtils.getDiceIcon(newDice.getFirstDice()));
			lbl_dice2
					.setIcon(graphicUtils.getDiceIcon(newDice.getSecondDice()));

			lbl_dice1.setToolTipText("You launched " + newDice.toString());
			lbl_dice2.setToolTipText("You launched " + newDice.toString());

			btn_castDice.setEnabled(false);
			btn_ok.setEnabled(true);
			btn_ok.setToolTipText("");
			diceCast = true;
		}
	}

	/**
	 * Pressing ok just closes the window
	 * 
	 * @param evt
	 */
	private void btn_okActionPerformed(java.awt.event.ActionEvent evt) {
		close(ConfigurationConstGraphics.MINIMAL_CLOSE);
	}

	/**
	 * On closing declared dice are retrieved
	 */
	private void close(int close) {
		if (close == ConfigurationConstGraphics.MINIMAL_CLOSE) {
			newDiceDeclared = (Dice) cb_dice.getSelectedItem();
			this.setVisible(false);
			this.dispose();
		} else if (close == ConfigurationConstGraphics.TOTAL_CLOSE) {
			parent.close();
		}
	}

	/**
	 * Generates the list of valid values, using {@link #dependingDice}, that
	 * will be displayed in the combobox
	 */
	private void getListForComboBox() {
		if (dependingDice == null) {
			dependingDice = Dice.getMinimumDice();
			listOfDice = dependingDice.getBiggerEqValues();
		} else {
			listOfDice = dependingDice.getBiggerValues();
		}
	}

	/**
	 * Simple function that returns a new CoveredDice holding the values the
	 * player declared in this dialog window. Calling this is the way to
	 * retrieve this value from here. The player to be associated with the
	 * coveredDice has to be passed.
	 * 
	 * @param p
	 *            the player who cast the dice inside this dialog, that will be
	 *            associated with the returned new CoveredDice
	 * @return
	 */
	public CoveredDice getCoveredDice(PlayerId p) {
		return new CoveredDice(newDiceDeclared, newDice, p);
	}
}
